//
//  HZMBannerDetailsVC.m
//  糖糖大魔王
//
//  Created by 韩震明 on 15/10/23.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "HZMBannerDetailsVC.h"
#import "HZMTableDetailsVC.h"
#import "HZMTableDetailsType1VC.h"

@interface HZMBannerDetailsVC ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain) UITableView *tableView;
@property(nonatomic, retain) NSMutableArray *topicArr;

@end

@implementation HZMBannerDetailsVC

- (void)dealloc
{
    [_bannerModel release];
    [_tableView release];
    [_topicArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNavigation];
    
    [self createTableView];
    
    [self createData];
    
}


#pragma mark TableView Setting
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.tableView.rowHeight = 150;
    self.tableView.separatorStyle = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
}

#pragma mark TableView Delegate Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.topicArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    HZMTopicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[HZMTopicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
    }
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:[self.topicArr[indexPath.row] pic]] placeholderImage:[UIImage imageNamed:@"back.gif"]];
    cell.titleLabel.text = [self.topicArr[indexPath.row] title];
    cell.likeImageView.image = [UIImage imageNamed:@"like.png"];
    cell.likeLabel.text = [self.topicArr[indexPath.row] likes];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[self.topicArr[indexPath.row] cellType] isEqualToString:@"0"]) {
        HZMTableDetailsVC *tableVC = [[HZMTableDetailsVC alloc] init];
        [self.navigationController pushViewController:tableVC animated:YES];
        tableVC.urlStr = [NSString stringWithFormat:@"http://open3.bantangapp.com/topic/info?app_installtime=1445332782.662591&app_versions=4.2.3&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&id=%@&oauth_token=bf8be334eb1fd06b1bf65f5883dd415a&os_versions=9.1&screensize=1242&statistics_uv=1&track_device_info=iPhone72C1&track_deviceid=2F83AC09-B470-4C43-9DEE-E0FB95B4B438&track_user_id=1457891&v=7", [self.topicArr[indexPath.row] cellId]];
    } else {
        HZMTableDetailsType1VC *type = [[HZMTableDetailsType1VC alloc] init];
        type.strUrl = [NSString stringWithFormat:@"http://open3.bantangapp.com/topic/info?app_installtime=1445332782.662591&app_versions=4.2.3&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&id=%@&oauth_token=bf8be334eb1fd06b1bf65f5883dd415a&os_versions=9.1&screensize=1242&statistics_uv=1&track_device_info=iPhone72C1&track_deviceid=2F83AC09-B470-4C43-9DEE-E0FB95B4B438&track_user_id=1457891&v=7", [self.topicArr[indexPath.row] cellId]];
        [self.navigationController pushViewController:type animated:YES];
    }
}



#pragma mark Navigation Setting
- (void)setNavigation
{
    self.navigationItem.title = self.bannerModel.title;
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(-20, 0, 40, 40);
    [leftButton setImage:[UIImage imageNamed:@"nav_back@2x.png"] forState:UIControlStateNormal];
    [self.view addSubview:leftButton];
    [leftButton addTarget:self action:@selector(leftBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:leftButton] autorelease];
    
    self.navigationController.navigationBar.translucent = NO;
}




#pragma mark Navigationbar ButtonAction
- (void)leftBarButtonClick:(UIBarButtonItem *)leftBarButton
{
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark Create Data
- (void)createData
{
    if ([self.bannerModel.bannerType isEqualToString:@"topic_list"]) {
        NSString *strURL = [NSString stringWithFormat:@"http://open3.bantangapp.com/topic/list?app_installtime=1445332782.662591&app_versions=4.2.3&channel_name=appStore&client_id=bt_app_ios&client_secret=9c1e6634ce1c5098e056628cd66a17a5&ids=%@&oauth_token=bf8be334eb1fd06b1bf65f5883dd415a&os_versions=9.1&page=0&pagesize=20&screensize=1242&track_device_info=iPhone72C1&track_deviceid=2F83AC09-B470-4C43-9DEE-E0FB95B4B438&track_user_id=1457891&v=7", [self.bannerModel.extend stringByReplacingOccurrencesOfString:@"," withString:@"%2C"]];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        [manager GET:strURL parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            NSDictionary *dic = responseObject;
            self.topicArr = [NSMutableArray array];
            self.topicArr = [HZMBannerDetails baseModelByArr:dic[@"data"][@"topic"]];
            [self.tableView reloadData];
        } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
            
            
            
        }];
        
    } else {
//        直接跳到baseWebView页面不用处理
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
